{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "OOP_Concepts.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyMusb1P4h7vyyYx0Khy08NO",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Divyanshu-ISM/Oil-and-Gas-data-analysis/blob/master/OOP_Concepts.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JwuG7INnIvYJ",
        "colab_type": "text"
      },
      "source": [
        "#Object Oriented Programming Tutorial."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "G_fqsSQ4Iin3",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "105619e3-7ddb-4a46-bbdd-e98084c96cdb"
      },
      "source": [
        "l = [1,2,3,4,5]\n",
        "\n",
        "l.count(2)\n",
        "#So l is a list object, and list is a class. \n",
        "#count is a function defined inside that class."
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HOz8t0_LJeuw",
        "colab_type": "text"
      },
      "source": [
        "In Python Everything is an object. \n",
        "To check for the type of object something is, just pass it inside type()"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rdMgg9cTJA9i",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "3c7d63ff-ddae-4653-9cc3-c9bba4fc05fc"
      },
      "source": [
        "#For example -\n",
        "print(type(1))\n",
        "print(type([]))\n",
        "print(type(()))\n",
        "print(type({}))"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "<class 'int'>\n",
            "<class 'list'>\n",
            "<class 'tuple'>\n",
            "<class 'dict'>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cZoBBukUJygt",
        "colab_type": "text"
      },
      "source": [
        "##How can we create our own Object types? That is where the class keyword comes in."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TAbfL8Z5Jt-p",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "1c2dec2d-61ad-4601-aa92-b1d6dde23b91"
      },
      "source": [
        "class Dog:\n",
        "\n",
        "  def __init__(self,name):\n",
        "    self.name = name\n",
        "\n",
        "d1 = Dog('Tommy')\n",
        "d2 = Dog(name = 'Rocco')\n",
        "\n",
        "print(f'The name of doggy d1 is {d1.name}')\n",
        "print(f'The name of doggy d2 is {d2.name}')"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "The name of doggy d1 is Tommy\n",
            "The name of doggy d2 is Rocco\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NedzFBKbL7We",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 170
        },
        "outputId": "fcfd7589-f042-4b8b-f7ae-e03cdb704a92"
      },
      "source": [
        "class Rock:\n",
        "\n",
        "  type = 'Sedimentary Rock'\n",
        "\n",
        "  def __init__(self,lith,poro,perm):\n",
        "\n",
        "    self.poro = poro\n",
        "    self.lith = lith\n",
        "    self.perm = perm\n",
        "\n",
        "r1 = Rock('SST',0.25,150)\n",
        "r2 = Rock('LST',0.08,40)\n",
        "\n",
        "#This is a class Attribute\n",
        "print(f'Rock (be it any of this class) is a {r1.type}')\n",
        "\n",
        "#These are object Attributes\n",
        "print(f'1. Lithology of Rock r1 : {r1.lith}')\n",
        "print(f'2. Porosity of Rock r1 : {r1.poro}')\n",
        "print(f'3. Permeability of Rock r1 : {r1.perm}')\n",
        "\n",
        "\n",
        "#These are object Attributes\n",
        "print(f'\\n\\n1. Lithology of Rock r2 : {r2.lith}')\n",
        "print(f'2. Porosity of Rock r2 : {r2.poro}')\n",
        "print(f'3. Permeability of Rock r2 : {r2.perm}')\n"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Rock (be it any of this class) is a Sedimentary Rock\n",
            "1. Lithology of Rock r1 : SST\n",
            "2. Porosity of Rock r1 : 0.25\n",
            "3. Permeability of Rock r1 : 150\n",
            "\n",
            "\n",
            "1. Lithology of Rock r2 : LST\n",
            "2. Porosity of Rock r2 : 0.08\n",
            "3. Permeability of Rock r2 : 40\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UY-D89L6QhAz",
        "colab_type": "text"
      },
      "source": [
        "#Class Methods \n",
        "\n",
        "The stuff that we passed into the class using the attributes, we can use it to perform computations and get a new value out. \n",
        "\n",
        "These computations and hence various such tasks that need to be performed on the class attributes, can be divided into several functions. \n",
        "\n",
        "These functions that are defined inside the class body, and use the class object's attributes to do computations/tasks are called class methods. "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fvMhiTlCR7bD",
        "colab_type": "text"
      },
      "source": [
        "###Example 1 : Circle Class."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YXa28STeNELm",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "09d281b8-b139-4d69-e321-c4a342baebcb"
      },
      "source": [
        "class Circle:\n",
        "\n",
        "  pi = 3.14\n",
        "\n",
        "  #initialize and obtain attributes in class body \n",
        "  def __init__(self,radius = 1):\n",
        "    self.radius = radius\n",
        "\n",
        "  #Now we have our radius, so we can create functions to compute stuff using r. \n",
        "  #These functions are called methods since they are particular to a class. \n",
        "  def area(self):\n",
        "    a = (self.pi)*(self.radius)**2\n",
        "    return a\n",
        "  \n",
        "  def circumference(self):\n",
        "    return 2*(self.pi)*(self.radius)\n",
        "\n",
        "c1 = Circle(20)\n",
        "\n",
        "print(f'1. The radius of c1 is {c1.radius}')\n",
        "\n",
        "print(f'2. The Calculated Area of c1 is {c1.area()}')\n",
        "\n",
        "print(f'3. The Calculated CircumFerence of c1 is {c1.circumference()}')"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1. The radius of c1 is 20\n",
            "2. The Calculated Area of c1 is 1256.0\n",
            "3. The Calculated CircumFerence of c1 is 125.60000000000001\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ooUWGuE_V0IJ",
        "colab_type": "text"
      },
      "source": [
        "###Example 2 : Rock PermPredictor class."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2wNMvGSEUcnV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "b69b7341-aee7-4f54-f297-8c6bfdcf1271"
      },
      "source": [
        "class PermPredictor :\n",
        "\n",
        "  def __init__(self,swc,phi,r):\n",
        "    self.swc = swc\n",
        "    self.phi = phi\n",
        "    self.r = r\n",
        "  \n",
        "  def hagenpoiseulle(self):\n",
        "\n",
        "    k = (self.phi)*(self.r)**2/8\n",
        "\n",
        "    return k \n",
        "\n",
        "  def timur(self):\n",
        "\n",
        "    k = (8.5*(self.phi)**4.4)/(self.swc)**2\n",
        "    return k\n",
        "\n",
        "\n",
        "rock1 = PermPredictor(0.10 , 0.20, 3)\n",
        "\n",
        "print(rock1.hagenpoiseulle())\n",
        "print(rock1.timur())"
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.225\n",
            "0.7144155627978243\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8QB6BBlyYJbb",
        "colab_type": "text"
      },
      "source": [
        "To use any class/object attribute around in the class inside various class methods, we gotta use it with the self. keyword ahead of it."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tLXvKgiEWv6f",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#######"
      ],
      "execution_count": 28,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "z1PePVmmYuVA",
        "colab_type": "text"
      },
      "source": [
        "#Inheritence \n",
        "\n",
        "A way to extend the functionality of a base class by passing it onto the derived class, in which we can add more stuff."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "abT0BBLzYs-5",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "41ae472e-bece-4af4-b38d-21cbce074526"
      },
      "source": [
        "class Greeting : \n",
        "\n",
        "  def __init__(self):\n",
        "\n",
        "    print('Hello! The Greeting class has been created. Good Day!')\n",
        "\n",
        "  def smalltalk(self):\n",
        "    print('So! How is life?')\n",
        "\n",
        "  def weather(self):\n",
        "    print('It is hot today!')\n",
        "\n",
        "\n",
        "\n",
        "class SayName(Greeting):\n",
        "\n",
        "  def __init__(self):\n",
        "\n",
        "    Greeting.__init__(self)\n",
        "    print('Hello Divyanshu!')\n",
        "\n",
        "  #we can also over ride intial functionality. \n",
        "  # def weather(self):\n",
        "  #   print('It is rainy today!')\n",
        "\n",
        "  #we can also extend functionality.\n",
        "  def askforwater(self):\n",
        "    print('Do you want a glass of water?')\n",
        "\n",
        "o1 = SayName()\n",
        "\n",
        "o1.weather()\n",
        "\n",
        "o1.askforwater()"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hello! The Greeting class has been created. Good Day!\n",
            "Hello Divyanshu!\n",
            "It is hot today!\n",
            "Do you want a glass of water?\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xAPyJRimZPep",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "ecd4711c-af1a-4d16-8672-5dff8e0395ad"
      },
      "source": [
        "class Greeting : \n",
        "\n",
        "  def __init__(self):\n",
        "\n",
        "    print('Hello! The Greeting class has been created. Good Day!')\n",
        "\n",
        "  def smalltalk(self):\n",
        "    print('So! How is life?')\n",
        "\n",
        "  def weather(self):\n",
        "    print('It is hot today!')\n",
        "\n",
        "\n",
        "\n",
        "class SayName(Greeting):\n",
        "\n",
        "  def __init__(self):\n",
        "\n",
        "    #This initialclass.__init__ is the key part of inheritence.\n",
        "    Greeting.__init__(self)\n",
        "    print('Hello Divyanshu!')\n",
        "\n",
        "  #we can also over ride intial functionality. \n",
        "  def weather(self):\n",
        "    print('It is rainy today!')\n",
        "\n",
        "  #we can also extend functionality.\n",
        "  def askforwater(self):\n",
        "    print('Do you want a glass of water?')\n",
        "\n",
        "o1 = SayName()\n",
        "\n",
        "o1.weather()\n",
        "\n",
        "o1.askforwater()"
      ],
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hello! The Greeting class has been created. Good Day!\n",
            "Hello Divyanshu!\n",
            "It is rainy today!\n",
            "Do you want a glass of water?\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gGVrSSkVa4Jv",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        },
        "outputId": "e738740b-8abb-4e21-f7fe-192a8d28a25f"
      },
      "source": [
        "class Greeting : \n",
        "\n",
        "  def __init__(self):\n",
        "\n",
        "    print('Hello! The Greeting class has been created. Good Day!')\n",
        "\n",
        "  def smalltalk(self):\n",
        "    print('So! How is life?')\n",
        "\n",
        "  def weather(self):\n",
        "    print('It is hot today!')\n",
        "\n",
        "\n",
        "\n",
        "class SayName(Greeting):\n",
        "\n",
        "  def __init__(self):\n",
        "\n",
        "    #This initialclass.__init__ is the key part of inheritence.\n",
        "    Greeting.__init__(self)\n",
        "    print('Hello! Divyanshu! SayName Class has been Created.')\n",
        "\n",
        "  #we can also over ride intial functionality. \n",
        "  def weather(self):\n",
        "    print('It is rainy today!')\n",
        "\n",
        "  #we can also extend functionality.\n",
        "  def askforwater(self):\n",
        "    print('Do you want a glass of water?')\n",
        "\n",
        "\n",
        "class TalkMath(SayName):\n",
        "\n",
        "  def __init__(self):\n",
        "\n",
        "    # Greeting.__init__(self)\n",
        "    SayName.__init__(self)\n",
        "    print('Finally, TalkMath has been created. Would you like to talk some math?')\n",
        "\n",
        "\n",
        "o2 = TalkMath()\n",
        "\n",
        "o2.askforwater()\n",
        "\n",
        "o2.smalltalk()\n",
        "\n",
        "o2.weather()"
      ],
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hello! The Greeting class has been created. Good Day!\n",
            "Hello! Divyanshu! SayName Class has been Created.\n",
            "Finally, TalkMath has been created. Would you like to talk some math?\n",
            "Do you want a glass of water?\n",
            "So! How is life?\n",
            "It is rainy today!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-GYJorB_bhir",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "###"
      ],
      "execution_count": 44,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Hrt7wP_8eLTg",
        "colab_type": "text"
      },
      "source": [
        "#PolyMorphism \n",
        "\n",
        "Dog also 'speaks'. \n",
        "\n",
        "Cat also 'speaks'.\n",
        "\n",
        "But Depending on who's speaking, the sound will be different. \n",
        "\n",
        "Dog --> speak ---> 'WOOF!!'\n",
        "\n",
        "Cat --> speak --> 'MEOW!!'\n",
        "\n",
        "Exactly!\n",
        "\n",
        "Polymorphism is a way in which a method (function) with the same name, say function speak() , behaves differently, based on the class it belongs to. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aVa8sI6Jd9Lc",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "0787bfd1-cb97-42f2-e05f-7e6217ff108f"
      },
      "source": [
        "class Dog:\n",
        "\n",
        "  def __init__(self,name):\n",
        "    self.name = name\n",
        "\n",
        "  def speak(self):\n",
        "    print(f'{self.name} says WOOF!')\n",
        "\n",
        "class Cat: \n",
        "\n",
        "  def __init__(self, name):\n",
        "    self.name = name \n",
        "\n",
        "  def speak(self):\n",
        "    print(f'{self.name} says Meowww!')\n",
        "\n",
        "d = Dog('Tommey')\n",
        "\n",
        "c = Cat('Felix')\n",
        "\n",
        "d.speak()\n",
        "\n",
        "c.speak()"
      ],
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Tommey says WOOF!\n",
            "Felix says Meowww!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "htR808YVf13X",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "####"
      ],
      "execution_count": 48,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RDtXBkH_hFfr",
        "colab_type": "text"
      },
      "source": [
        "#A short Introduction to Special Class Functions (Or DunDer Functions).\n",
        "\n",
        "> Dunder -- Double Underscore (__)\n",
        "\n",
        "For example, in list objects you can see 2 cases :-\n",
        "\n",
        ">1. listname.count('a')\n",
        "\n",
        ">2. len(listname)\n",
        "\n",
        "\n",
        "So, count is a result of normal class method. \n",
        "\n",
        "len, which can be used as a function now, is a result of Special/Dunder fn."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3qHtrOvQhEIb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class Sentence:\n",
        "\n",
        "  def __init__(self , phrase):\n",
        "    self.phrase = phrase \n",
        "\n",
        "  #This is a normal class method. \n",
        "  def sayhi(self):\n",
        "    print('Hey! This is normal')\n",
        "\n",
        "  def __str__(self):\n",
        "    return 'This is Special!'\n",
        "\n",
        "\n",
        "ob = Sentence('This is a sentence!')"
      ],
      "execution_count": 57,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SdnB838AiM3b",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a865f9ff-96c9-418e-fd1b-9cc01fe7ee57"
      },
      "source": [
        "ob.sayhi()"
      ],
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hey! This is normal\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Dw3mlXyiiPEv",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 163
        },
        "outputId": "957c7a00-c4d9-4f74-ffdc-25f469cfa9f5"
      },
      "source": [
        "ob.spcl()"
      ],
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "error",
          "ename": "AttributeError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-55-dde42d011a96>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mob\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspcl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mAttributeError\u001b[0m: 'Sentence' object has no attribute 'spcl'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Tg0NaG1NiR3h",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "fc9bfa00-1e5e-4f18-eab4-82cff6c2487a"
      },
      "source": [
        "str(ob)"
      ],
      "execution_count": 58,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'This is Special!'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 58
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8mlxvs76iVLP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#We cannot really create custom dunder functions."
      ],
      "execution_count": 59,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oO6_tIzVi_qc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}